package com.madou.yy.football.obstacle;

import com.madou.yy.football.BackgroundPanel;
import com.madou.yy.football.ResourcesUtil;

import javax.swing.*;
import java.awt.*;

public class Goalkeeper extends JLabel implements Obstacle,Runnable {

    boolean isAdd = true;
    BackgroundPanel backgroundPanel;
    Thread thread = null;
    public Goalkeeper(BackgroundPanel backgroundPanel){
        this.backgroundPanel = backgroundPanel;
        this.setBounds(backgroundPanel.getWidth()/2,100,78,128);
        this.setPreferredSize(new Dimension(78,128));
        this.setIcon(new ImageIcon(ResourcesUtil.getRootPath()+"\\ball\\smy.png"));
    }

    @Override
    public String name() {
        return "守门员";
    }

    @Override
    public JComponent getComponent() {
        return this;
    }

    public void start(){
        thread = new Thread(this);
        thread.start();
    }

    public void stop(){
        thread.stop();
        thread = null;
    }

    public void move() {
        try {
            Rectangle bounds = this.getBounds();
            double width = bounds.getX();
            if (isAdd) {
                width += 30;
                if (width < backgroundPanel.getWidth() * 4 / 5) {
                    backgroundPanel.repaint();
                    this.setBounds((int) width, bounds.y, bounds.width, bounds.height);
                    Thread.sleep(50);
                } else {
                    isAdd = false;
                }
            } else {
                width -= 30;
                if (width > backgroundPanel.getWidth() * 1 / 5) {
                    backgroundPanel.repaint();
                    this.setBounds((int) width, bounds.y, bounds.width, bounds.height);
                    Thread.sleep(100);
                } else {
                    isAdd = true;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true&&!Thread.interrupted()){
            move();
        }
    }
}
